package com.hellobike.haxing.modules.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.system.util.JwtUtil;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.entity.City;
import com.hellobike.haxing.modules.product.entity.District;
import com.hellobike.haxing.modules.product.entity.Province;
import com.hellobike.haxing.modules.product.entity.Street;
import com.hellobike.haxing.modules.product.mapper.CityMapper;
import com.hellobike.haxing.modules.product.mapper.DistrictMapper;
import com.hellobike.haxing.modules.product.mapper.ProvinceMapper;
import com.hellobike.haxing.modules.product.mapper.StreetMapper;
import com.hellobike.haxing.modules.system.entity.SysDepart;
import com.hellobike.haxing.modules.system.service.ISysDepartService;
import com.hellobike.haxing.modules.wms.entity.Warehouse;
import com.hellobike.haxing.modules.wms.entity.WarehouseDepart;
import com.hellobike.haxing.modules.wms.mapper.WarehouseDepartMapper;
import com.hellobike.haxing.modules.wms.service.IWarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: t_wms_warehouse
 * @Author: haxing-boot
 * @Date: 2020-02-08
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/warehouse")
@Slf4j
public class WarehouseController extends BaseController<Warehouse, IWarehouseService> {
    @Autowired
    private IWarehouseService warehouseService;

    @Autowired
    private ISysDepartService sysDepartService;


    @Autowired
    private WarehouseDepartMapper warehouseDepartMapper;

    /**
     * 分页列表查询
     *
     * @param warehouse
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "t_wms_warehouse-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(Warehouse warehouse,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Warehouse> queryWrapper = QueryGenerator.initQueryWrapper(warehouse, req.getParameterMap());
        Page<Warehouse> page = new Page<Warehouse>(pageNo, pageSize);
        IPage<Warehouse> pageList = warehouseService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param warehouse
     * @return
     */
    @AutoLog(value = "t_wms_warehouse-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Warehouse warehouse, HttpServletRequest request) {
        Warehouse param = new Warehouse();
        param.setWarehouseCode(warehouse.getWarehouseCode());
        if (warehouseService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
            return Result.error("仓库编码已被占用");
        }
        Warehouse param2 = new Warehouse();
        param2.setWarehouseName(warehouse.getWarehouseName());
        if (warehouseService.getBaseMapper().selectOne(new QueryWrapper<>(param2)) != null) {
            return Result.error("仓库名称已被占用");
        }
        warehouseService.save(warehouse);
        try {
            String username = JwtUtil.getUserNameByToken(request);
            SysDepart sysDepart = new SysDepart();
            sysDepart.setParentId(warehouse.getDepartId());
            sysDepart.setDepartName(warehouse.getWarehouseName());
            sysDepart.setDeleted(0);
            sysDepart.setOrgCategory("12");
            WarehouseDepart warehouseDepart = new WarehouseDepart();
            warehouseDepart.setDepartId(sysDepart.getId());
            sysDepartService.saveDepartData(sysDepart, username);
            warehouseDepart.setDepartCode(sysDepart.getOrgCode());
            warehouseDepartMapper.insert(warehouseDepart);
        } catch (Exception ex) {

        }
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param warehouse
     * @return
     */
    @AutoLog(value = "t_wms_warehouse-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Warehouse warehouse) {
        Warehouse warehouse1 = warehouseService.getById(warehouse.getId());
        if (!warehouse1.getWarehouseCode().equals(warehouse.getWarehouseCode())) {
            Warehouse param = new Warehouse();
            param.setWarehouseCode(warehouse.getWarehouseCode());
            if (warehouseService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
                return Result.error("仓库编码已被占用");
            }
        }
        if (!warehouse1.getWarehouseName().equals(warehouse.getWarehouseName())) {
            Warehouse param = new Warehouse();
            param.setWarehouseName(warehouse.getWarehouseName());
            if (warehouseService.getBaseMapper().selectOne(new QueryWrapper<>(param)) != null) {
                return Result.error("仓库名称已被占用");
            }
        }
        warehouseService.updateById(warehouse);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "t_wms_warehouse-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        warehouseService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "t_wms_warehouse-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.warehouseService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Warehouse warehouse = warehouseService.getById(id);
        if (warehouse == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(warehouse);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param warehouse
     */
    @AutoLog(value = "t_wms_warehouse-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Warehouse warehouse) {
        return super.exportXls(request, warehouse, Warehouse.class, "t_wms_warehouse");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "t_wms_warehouse-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Warehouse.class);
    }


    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private StreetMapper streetMapper;

    @GetMapping(value = "/getProvince")
    public Result<?> getProvince() {
        List<Province> provinces = provinceMapper.selectList(new QueryWrapper<>());
        return Result.ok(provinces);
    }

    @GetMapping(value = "/getCity")
    public Result<?> getCity(@RequestParam(name = "id", required = true) Long id) {
        City param = new City();
        param.setProvinceId(id);
        QueryWrapper<City> queryWrapper = new QueryWrapper<>(param);
        List<City> cities = cityMapper.selectList(queryWrapper);
        return Result.ok(cities);
    }

    @GetMapping(value = "/getDistrict")
    public Result<?> getDistrict(@RequestParam(name = "id", required = true) Long id) {
        District param = new District();
        param.setCityId(id);
        QueryWrapper<District> queryWrapper = new QueryWrapper<>(param);
        List<District> cities = districtMapper.selectList(queryWrapper);
        return Result.ok(cities);
    }

    @GetMapping(value = "/getStreet")
    public Result<?> getStreet(@RequestParam(name = "id", required = true) Long id) {
        Street param = new Street();
        param.setDistrictId(id);
        QueryWrapper<Street> queryWrapper = new QueryWrapper<>(param);
        List<Street> cities = streetMapper.selectList(queryWrapper);
        return Result.ok(cities);
    }

}
